<?php
namespace app\api\controller;

use think\Controller;
use app\api\model\Postdatadb;
use app\api\model\Thirddb;

class Trtcapi extends Controller
{

    public $request = array();

    protected $appid = '4h3mof3hz9dtaa3v';

    protected $appSecret = 'qoq3do2tjj46mq34tjwdulxgvec55blz8428gydm25d2pn3s1fl7gxxutfwfxl9x';

    protected $geoUrl = 'https://apis.map.qq.com/ws/geocoder/v1/?address=';

    protected $geoKey = 'TOABZ-KN3WO-5CNWI-SXPQ5-WQN3K-FKFZV';

    protected $wsdl = 'http://10.68.0.65:8080/OssWeb/services/CallCenterForGzInterface?wsdl';

    public $soap_client;

    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");
        header('Content-Type:application/json; charset=utf-8');
        $request = request();
        $this->request = $request->param();
        $postdatadb = new Postdatadb();
        $postdatadb->param['post_json'] = $this->request;
        $postdatadb->param['post_url'] = $request->url();
        $postdatadb->ajaxSave();
    }

    public function index()
    {
        exit('欢迎使用');
    }

    public function getUserInfo($mac_id)
    {
        $this->soap_client = new \SoapClient($this->wsdl);
    }

    public function getFunction()
    {
        $opts = array(
            'http' => array(
                'user_agent' => 'PHPSoapClient'
            ),
            'ssl' => array(
                'verify_peer' => false
            )
        );
        $context = stream_context_create($opts);
        $this->soap_client = new \SoapClient($this->wsdl, array(
            "trace" => 1,
            "cache_wsdl" => WSDL_CACHE_NONE,
            "exception" => 1,
            'stream_context' => $context
        ));
        $res = $this->soap_client->__getFunctions();
        dump($res);
    }

    private function chkPassword()
    {
        $data = $this->request;
        if (! isset($data['signature'])) {
            $return['code'] = 0;
            $return['msg'] = '准进指纹错误';
            $this->ajaxReturn($return);
        }
        $signature = $data['signature'];
        $str = md5($data['orderid'] . $data['trtc']['room_id'] . $data['trtc']['user_id'] . $data['timeStamp'] . $this->appSecret);
        if (strtolower($str) != $signature) {
            $return['code'] = 0;
            $return['msg'] = '签名错误';
            $this->ajaxReturn($return);
        }
    }

    protected function ajaxReturn($return, $json_option = 0)
    {
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($return, $json_option));
    }
    // 地址转坐标
    protected function geocoder($addr)
    {
        $data = file_get_contents($this->geoUrl . $addr . '&key=' . $this->geoKey);
        return json_decode($data, true);
    }
    // 交换盒子信息
    public function getBoxInfo()
    {
        if (! isset($this->request['mac_id']) || ! $this->request['mac_id']) {
            $return['code'] = 0;
            $return['msg'] = '机器识别码错误';
            $this->ajaxReturn($return);
        }
        $return['code'] = 1;
        $return['data']['phone'] = '18900000000';
        $return['data']['addr'] = '贵州省贵阳市观山湖区绿地.联盛国际10号楼';
        $get_geo = $this->geocoder($return['data']['addr']);
        if ($get_geo['status'] == 0) {
            $return['data']['province'] = $get_geo['result']['address_components']['province'];
            $return['data']['city'] = $get_geo['result']['address_components']['city'];
            $return['data']['district'] = $get_geo['result']['address_components']['district'];
            $return['data']['lng'] = $get_geo['result']['location']['lng'];
            $return['data']['lat'] = $get_geo['result']['location']['lat'];
        }
        $this->ajaxReturn($return);
    }
    // 邀请第三方通话
    public function pushThird()
    {
        $this->chkPassword();
        $thirddb = new Thirddb();
        $data = $this->request;
        $thirddb->param['order_id'] = $data['orderid'];
        $thirddb->param['room_id'] = $data['trtc']['room_id'];
        $thirddb->param['address'] = '01010101080808080802';
        $thirddb->param['post_json'] = json_encode($this->request);
        $thirddb->ajaxSave();
        $return['code'] = 1;
        $return['msg'] = 'success';
        $this->ajaxReturn($return);
    }
    // 查询三方通话
    public function getThird()
    {
        if (! isset($this->request['mac_id'])) {
            $return['code'] = 0;
            $return['msg'] = '参数错误';
            $this->ajaxReturn($return);
        }
        $thirddb = new Thirddb();
        $thirddb->param['address'] = $this->request['mac_id'];
        $thirddb->param['state'] = 'yes';
        $data = $thirddb->ajaxFind();
        if (count($data) > 0) {
            // unset($data['post_json']);
            // unset($data['state']);
            $data['post_json'] = json_decode($data['post_json']);
            $return['data'] = $data;
            $return['code'] = 1;
        } else {
            $return['msg'] = '无邀请信息';
            $return['code'] = 0;
        }
        $this->ajaxReturn($return);
    }
    // 设置通话状态
    public function setThirdRead()
    {
        if (! isset($this->request['third_id'])) {
            $return['code'] = 0;
            $return['msg'] = '参数错误';
            $this->ajaxReturn($return);
        }
        if (! isset($this->request['state'])) {
            $return['code'] = 0;
            $return['msg'] = '参数错误';
            $this->ajaxReturn($return);
        }
        $thirddb = new Thirddb();
        $thirddb->param['third_id'] = $this->request['third_id'];
        $thirddb->param['state'] = $this->request['state'];
        $thirddb->ajaxSave();
        $return['code'] = 1;
        $return['msg'] = 'success';
        $this->ajaxReturn($return);
    }
    // 民警端退出通话
    public function outTrtc(){
        if (! isset($this->request['mac_id'])) {
            $return['code'] = 0;
            $return['msg'] = '参数错误';
            $this->ajaxReturn($return);
        }
        $return['code'] = 1;
        $return['msg'] = 'success';
        $this->ajaxReturn($return);
    }
}